package com.chhuang.ewcrm.thread;

import com.chhuang.ewcrm.entity.EBuy;
import com.chhuang.ewcrm.entity.EOther;
import com.chhuang.ewcrm.entity.ESell;
import com.chhuang.ewcrm.entity.ETransfer;
import com.chhuang.ewcrm.utils.MyPath;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 * @author chhuang
 * @create 2020-11-19-11:14
 */
public class PrintWeighRecordThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(PrintWeighRecordThread.class);

    private EBuy eBuy;
    private ESell eSell;
    private ETransfer eTransfer;
    private EOther eOther;

    public PrintWeighRecordThread(EBuy eBuy){
        this.eBuy = eBuy;
    }

    public PrintWeighRecordThread(ESell eSell){
        this.eSell = eSell;
    }

    public PrintWeighRecordThread(ETransfer eTransfer){
        this.eTransfer = eTransfer;
    }

    public PrintWeighRecordThread(EOther eOther){
        this.eOther = eOther;
    }

    @Override
    public void run() {
        if(eBuy!=null){
            logger.debug("打印采购过磅单");
            //打印过磅记录
            String reportPath = MyPath.getJasperPath()+"EBuy.jasper";
            //isUseFieldDescription 默认为true, 为true时会用description对应的值获取javabean的属性
            JRBeanArrayDataSource beanArrayDataSource = new JRBeanArrayDataSource(new EBuy[]{eBuy}, false);
            print(reportPath, beanArrayDataSource);
        }

        if(eSell!=null){
            logger.debug("打印销售过磅单");
            String reportPath = MyPath.getJasperPath()+"ESell.jasper";
            JRBeanArrayDataSource beanArrayDataSource = new JRBeanArrayDataSource(new ESell[]{eSell});
            print(reportPath, beanArrayDataSource);
        }

        if(eTransfer!=null){
            logger.debug("打印内部周转过磅单");
            String reportPath = MyPath.getJasperPath()+"ETransfer.jasper";
            JRBeanArrayDataSource beanArrayDataSource = new JRBeanArrayDataSource(new ETransfer[]{eTransfer});
            print(reportPath, beanArrayDataSource);
        }

        if(eOther!=null){
            logger.debug("打印其它过磅单");
            String reportPath = MyPath.getJasperPath()+"EOther.jasper";
            JRBeanArrayDataSource beanArrayDataSource = new JRBeanArrayDataSource(new EOther[]{eOther});
            print(reportPath, beanArrayDataSource);
        }
    }


    private void print(String reportPath, JRDataSource dataSource){
        try {
            //模板中填充数据 参数 模板地址， Map中为parameters对应的内容这里没有， datasource
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap<>(), dataSource);
            JasperPrintManager.printReport(jasperPrint, false);//直接打印
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}
